package com.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2WebMvc;

@Configuration
@EnableSwagger2WebMvc
public class Knife4jConfig {
    private String title;
    private String description;
    private String basePackage;
    @Value("${server.servlet.context-path}")
    private String contextPath;

    public Knife4jConfig(String title, String description, String basePackage) {
        this.title = title;
        this.description = description;
        this.basePackage = basePackage;
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder().title(title).description(description).version("1.0").contact(new Contact("芝麻", "https://gitee.com/li-zhi1802", "zhima_kamen@163.com")).build();
    }

    @Bean
    public Docket docket() {
        return new Docket(DocumentationType.SWAGGER_2).groupName(contextPath.substring(1)).apiInfo(apiInfo()).select()
                //只显示api路径下的页面
                .apis(RequestHandlerSelectors.basePackage(basePackage)).paths(PathSelectors.regex(String.format("%s/.*", contextPath))).build();
    }
}
